export const format = function(str, args = null) {
    if (!args) {
        return str;
    }

    let i = 0
    const REGEX = /\{(.+?)\}/g;
    return str.replace(REGEX,()=>{
        return  args[i++]
    });
}

/**
 * 保留小数点
 * @param value  val:传入值 n:保留几位
 */
export const formatFloat = (val, n=2)=>{
    if(!val) return 
    var value = Math.round(val*Math.pow(10,n))/Math.pow(10,n);
    var data = value.toString();
    var index = data.indexOf('.');   
    if (index < 0) {     
        data += '.';   
    } 
    for(var i = data.length - data.indexOf('.'); i <= n; i++){
        data += "0";
    }
    return data;
}

/**
 * 16位随机id
 */
export function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
